//
//  MTFansFollowViewController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/12.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTFansFollowViewController.h"
#import "MTUserTotalModel.h"
#import "MTUserListCell.h"
//#import "MTProfileViewController.h"
@interface MTFansFollowViewController ()
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray *userArray;

@property(nonatomic,strong) NSString *uri;
@end

@implementation MTFansFollowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(_type == MTFansFollowViewControllerFans)
    {
        self.title = @"我的粉丝";
        _uri = @"/user/get_my_fans_users";
    }else if(_type == MTFansFollowViewControllerFollow)
    {
        _uri = @"/user/get_my_follow_users";
        self.title = @"我的关注";
    }
    self.page = 1;
    self.tableView.rowHeight = 60;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.contentInset = UIEdgeInsetsMake(1, 0, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:@"MTUserListCell" bundle:nil] forCellReuseIdentifier:@"MTUserListCellID"];
    @weakify(self);
    // 下拉刷刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.page = 1;
        [self getDataIsHeaderRefresh:YES];
    }];
    // 上拉加载更多
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        
        self.page += 1;
        [self getDataIsHeaderRefresh:NO];
    }];
    self.tableView.mj_footer.automaticallyHidden = YES;
    
    
    [self getDataIsHeaderRefresh:YES];
    
}

#pragma mark - 请求所有数据方法
- (void)getDataIsHeaderRefresh:(BOOL)headerRefresh {
    @weakify(self);
    NSDictionary* requestDic = @{@"page":@(self.page)};
    [[MTHttpTool shareTool] postPath:_uri parameters:requestDic success:^(MTBaseData *datamodel) {
        MTUserTotalModel* model = [MTUserTotalModel mj_objectWithKeyValues:datamodel.data];
        if (headerRefresh) {
            self.userArray = model.list;
            [self.tableView reloadData];
        } else {
            [self.userArray addObjectsFromArray:model.list];
            [self.tableView reloadData];
        }
        if (model.last) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer resetNoMoreData];
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
    } failer:^(MTError *error) {
        @strongify(self);
        //        [self handleErrorWithErrorStr:error.errorDescription withHeaderRefresh:headerRefresh];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.userArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTUserListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MTUserListCellID"];
    cell.user = self.userArray[indexPath.row];
    if(_type == MTFansFollowViewControllerFollow)
    {
        cell.isFollow = YES;
    }else
    {
        cell.isFollow = NO;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MTUserModel *model = self.userArray[indexPath.row];
//    MTProfileViewController *profileVc = [[MTProfileViewController alloc] init];
//    profileVc.userId = model.userId;
//    [self.navigationController pushViewController:profileVc animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
